package com.fgs.common.widget.itemView;

import android.content.Context;
import android.content.res.TypedArray;
import android.text.TextUtils;
import android.util.AttributeSet;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.LinearLayout;

import androidx.annotation.Nullable;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

import com.fgs.common.R;
import com.fgs.common.adapter.CitySearchAdapter;
import com.fgs.common.entity.region.RegionInfo;
import com.fgs.common.utils.CommonUtil;
import com.fgs.common.utils.LogUtil;
import com.fgs.common.utils.MatchUtils;
import com.fgs.common.widget.dialog.CitySearchSelectDialog;
import com.google.gson.reflect.TypeToken;
import com.xuexiang.xutil.net.JsonUtil;
import com.xuexiang.xutil.resource.ResourceUtils;

import org.yczbj.ycrefreshviewlib.inter.OnItemClickListener;
import org.yczbj.ycrefreshviewlib.item.RecycleViewItemLine;

import java.util.ArrayList;
import java.util.List;

/**
 * @author fgs
 * @date 2021/2/23
 * @description
 **/
public class CitySearchView extends LinearLayout {
    //    private InputItemView inputItemView;
    private SelectInputItemView selectInputItemView;
    private RecyclerView recyclerView;
    private CitySearchAdapter citySearchAdapter;
    private OnCitySearchListener onCitySearchListener;
    private OnCitySearchSelectListener onCitySearchSelectListener;
    private final List<RegionInfo> resultList = new ArrayList<>();
    private List<RegionInfo> cityList = new ArrayList<>();

    private RegionInfo mRegionInfo;
    private boolean isNeedFilterCity = true;

    private CitySearchSelectDialog citySearchSelectDialog;

    public CitySearchView(Context context) {
        this(context, null);
    }

    public CitySearchView(Context context, @Nullable AttributeSet attrs) {
        this(context, attrs, 0);
    }

    public CitySearchView(Context context, @Nullable AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        initView(context, attrs);
    }

    private void initView(Context context, AttributeSet attrs) {

        initCityList();
        mRegionInfo = new RegionInfo();


        TypedArray typedArray = context.obtainStyledAttributes(attrs, R.styleable.CitySearchView);
        String title;
        String content;
        String contentHint;
        boolean isShowArrow;
        boolean isShowTip;
        try {
            title = typedArray.getString(R.styleable.CitySearchView_CitySearchTitle);
            content = typedArray.getString(R.styleable.CitySearchView_CitySearchContent);
            contentHint = typedArray.getString(R.styleable.CitySearchView_CitySearchContentHint);
            isShowArrow = typedArray.getBoolean(R.styleable.CitySearchView_CitySearchIsShowArrow, false);
            isShowTip = typedArray.getBoolean(R.styleable.CitySearchView_CitySearchIsShowTip, false);
        } finally {
            typedArray.recycle();
        }

        View view = LayoutInflater.from(context).inflate(R.layout.layout_city_search, null);
        LayoutParams layoutParams = new LayoutParams(LayoutParams.MATCH_PARENT, LayoutParams.WRAP_CONTENT);
        view.setLayoutParams(layoutParams);
        addView(view);
//        inputItemView = view.findViewById(R.id.layout_city_search_inputItemView);
        selectInputItemView = view.findViewById(R.id.layout_city_search_selectInputItemView);
        recyclerView = view.findViewById(R.id.layout_city_search_recyclerView);

        LinearLayoutManager linearLayoutManager = new LinearLayoutManager(getContext());
        linearLayoutManager.setOrientation(LinearLayoutManager.VERTICAL);
        recyclerView.setLayoutManager(linearLayoutManager);

        RecycleViewItemLine itemLine = new RecycleViewItemLine(getContext(),
                LinearLayout.HORIZONTAL,
                (int) CommonUtil.convertDpToPixel(1),
                getResources().getColor(R.color.colorLineGray));
        recyclerView.addItemDecoration(itemLine);

        citySearchAdapter = new CitySearchAdapter(context);
        recyclerView.setAdapter(citySearchAdapter);


        setTitle(title);
        setContent(content);
        setContentHint(contentHint);
        setIsShowArrow(isShowArrow);
        setIsShowTip(isShowTip);

        initListener();


    }

    private void setIsShowTip(boolean isShowTip) {
        selectInputItemView.setShowTip(isShowTip);
    }

    private void setIsShowArrow(boolean isShowArrow) {
        selectInputItemView.setShowArrow(isShowArrow);
    }

    public void setContentHint(String contentHint) {
        if (selectInputItemView != null && !TextUtils.isEmpty(contentHint)) {
            selectInputItemView.setContentHint(contentHint);
        }
    }

    private void initCityList() {

        new Thread(new Runnable() {
            @Override
            public void run() {
                String JsonData = ResourceUtils.readStringFromAssert("region.json");
                List<RegionInfo> regionInfoList = JsonUtil.fromJson(JsonData, new TypeToken<List<RegionInfo>>() {
                }.getType());
                cityList = regionInfoList;
            }
        }).start();

    }

    private void initListener() {
        selectInputItemView.setOnInputItemListener(new SelectInputItemView.OnInputItemListener() {
            @Override
            public void onInput(String string) {
                if (!TextUtils.isEmpty(string)) {
                    filterCity(string);
                } else {
                    clearCityList();
                }
            }
        });

        citySearchAdapter.setOnItemClickListener(new OnItemClickListener() {
            @Override
            public void onItemClick(int position) {
                int size = citySearchAdapter.getCount();
                if (position > size) {
                    return;
                }

                mRegionInfo = citySearchAdapter.getAllData().get(position);
                if (mRegionInfo != null) {
                    selectInputItemView.setContent(mRegionInfo.label);
                }

                if (!TextUtils.isEmpty(mRegionInfo.parentcode) && mRegionInfo.parentcode.equals("0")) {
                    mRegionInfo.parentcode = "";
                }
                if (TextUtils.isEmpty(mRegionInfo.county)) {
                    mRegionInfo.county = "";
                }

                if (onCitySearchListener != null) {
                    onCitySearchListener.citySearchCallback(mRegionInfo);
                }
                clearCityList();
            }
        });

        selectInputItemView.setOnSelectItemListener(new SelectInputItemView.OnSelectItemListener() {
            @Override
            public void onSelectClick() {
                showCitySelectDialog();
            }
        });
    }

    public void showCitySelectDialog() {
        if (citySearchSelectDialog == null) {
            citySearchSelectDialog = new CitySearchSelectDialog(getContext());
            citySearchSelectDialog.setOnCitySearchSelectListener(new CitySearchSelectDialog.OnCitySearchSelectListener() {
                @Override
                public void onCitySelect(RegionInfo regionInfo) {
                    mRegionInfo = regionInfo;
                    if (regionInfo != null && onCitySearchSelectListener != null) {
                        onCitySearchSelectListener.onCitySelect(regionInfo);
                        selectInputItemView.setContent(regionInfo.label);
                    }
                }
            });
        }
        citySearchSelectDialog.show();
    }

    private void clearCityList() {
        resultList.clear();
        citySearchAdapter.clear();
        citySearchAdapter.notifyDataSetChanged();
    }

    private void filterCity(String string) {
//        if (!isNeedFilterCity) {
//            return;
//        }
        resultList.clear();
        MatchUtils.find(string, cityList, resultList);
        int size = resultList.size();
        LogUtil.print("resultListSize", size + "");
        citySearchAdapter.clear();
        citySearchAdapter.addAll(resultList);
//        clearSelectCity();
    }

    private void clearSelectCity() {
        int size = citySearchAdapter.getCount();
        if (size == 0) {
            if (onCitySearchListener != null) {
                onCitySearchListener.citySearchCallback(new RegionInfo());
            }
        }
    }

    public void setTitle(String title) {
        selectInputItemView.setTitle(title);
    }

    public String getContent() {
        String content = selectInputItemView.getContent();
        return content;
    }

    public void setContent(String content) {
        if (selectInputItemView != null && !TextUtils.isEmpty(content)) {
            selectInputItemView.setContent(content);
        }
    }

    public void setIsNeedFilterCity(boolean isNeedFilterCity) {
        this.isNeedFilterCity = isNeedFilterCity;
    }

//    public void setCitySearchList(List<RegionInfo> infoList) {
//        citySearchAdapter.addAll(infoList);
//    }

    public void setOnCitySearchListener(OnCitySearchListener onCitySearchListener) {
        this.onCitySearchListener = onCitySearchListener;
    }

    public interface OnCitySearchListener {
        void citySearchCallback(RegionInfo regionInfo);
    }

    public interface OnCitySearchSelectListener {
        void onCitySelect(RegionInfo regionInfo);
    }

    public void setOnCitySearchSelectListener(OnCitySearchSelectListener onCitySearchSelectListener) {
        this.onCitySearchSelectListener = onCitySearchSelectListener;
    }
}
